//
//  SignatureViewController.m
//  Today
//
//  Created by dllo on 15/5/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "SignatureViewController.h"

@interface SignatureViewController ()

@end

@implementation SignatureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    activity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    activity.color = [UIColor grayColor];
    activity.frame = CGRectMake(0, 0, 200, 200);
    activity.center = self.view.center;
    [self.view addSubview:activity];
    [activity startAnimating];
    [self loadrequest];
    
    
    // Do any additional setup after loading the view.
}
-(void)Mytableview
{
    tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    [self.view addSubview:tableview];
    tableview.dataSource = self;
    tableview.delegate = self;
    [tableview release];
}
//加载内容（网络请求， 解析）
-(void)loadrequest
{
    NSString *str = @"http://qianming.sinaapp.com/index.php/AndroidApi23/index/cid/qutu/p/1/markId/0";
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
    request.HTTPMethod = @"GET";
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
        [activity stopAnimating];
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *array = [dic objectForKey:@"rows"];
        self.array = [NSMutableArray array];
        for (NSDictionary *dic1 in array) {
            model = [SignatureModel getSignatureModel];
            model.title = [dic1 objectForKey:@"title"];
            NSLog(@"%@", model.title);
            model.pic = [dic1 objectForKey:@"pic"];
            model.timeStr = [dic1 objectForKey:@"timeStr"];
            NSLog(@"%@", model.timeStr);
            model.pic_h = [dic1 objectForKey:@"pic_h"];
            [self.array addObject:model];
            NSLog(@"%ld", self.array.count);
        }
        
        //    用cell来显示内容
        [self Mytableview];
    }];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"cell";
    SignatureTableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell = [[[SignatureTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str]autorelease];
    }
    SignatureModel *Model = [self.array objectAtIndex:indexPath.row];
    cell.Model = Model;
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 300;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
